﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Core;

namespace Behaviors
{
    /// <summary>
    /// Fires when associated auto complete box becomes empty.
    /// </summary>
    public class AutocompleteEmptyTrigger : TriggerBase<AutoCompleteBox>
    {
        public AutocompleteEmptyTrigger()
        {
        }

        /// <summary>
        /// Get our thing hooked up
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.TextChanged += new RoutedEventHandler(AssociatedObject_TextChanged);
        }

        /// <summary>
        /// Clean up!
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.TextChanged -= new RoutedEventHandler(AssociatedObject_TextChanged);
        }

        /// <summary>
        /// The text has changed. See if we are empty now? If so, fire!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AssociatedObject_TextChanged(object sender, RoutedEventArgs e)
        {
            if (AssociatedObject.Text == null || AssociatedObject.Text == "")
            {
                InvokeActions(null);
            }
        }
    }
}
